<template>
  <div class="business_edit">
    <titleDiv
      :title="title"
      :isShow="true"
      text1="创建人："
      text2="创建时间："
      :value1="name"
      :value2="time"
    ></titleDiv>
    <div class="main">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
				<el-form-item label="设备类型:" prop="equipment">
					<h3 v-if="equipmentType == 'led' " class="name">LED屏</h3>
					<h3 v-else-if="equipmentType == 'hjq' " class="name">呼叫器</h3>
					<h3 v-else-if="equipmentType == 'pjq' " class="name">评价器</h3>
					<h3 v-else-if="equipmentType == 'qhj' " class="name">取号机</h3>
					<h3 v-else-if="equipmentType == 'dp' " class="name">电视机</h3>
				</el-form-item>
        <el-form-item label="设备名称:" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="设备名称"></el-input>
        </el-form-item>
				<el-form-item label="地址码:" prop="addressCode">
					<el-input v-model="form.addressCode"  placeholder="地址码"></el-input>
				</el-form-item>
				<el-form-item label="设备品牌:">
					<el-input v-model="form.brand" placeholder="设备品牌"></el-input>
				</el-form-item>
				<el-form-item label="厂家名称:">
					<el-input v-model="form.manufacturers" placeholder="厂家名称"></el-input>
				</el-form-item>
				<el-form-item label="设备型号:">
					<el-input v-model="form.unitType" placeholder="设备型号"></el-input>
				</el-form-item>
        <el-form-item label="备注:">
          <el-input v-model="form.remark" class="textArea" type="textarea" placeholder="" maxlength="500"></el-input>
        </el-form-item>
        <el-button class="btn" type="primary" @click="onSubmit('form')">提交</el-button>
      </el-form>
    </div>
  </div>
</template>
<script>
  import titleDiv from './view/titleDiv.vue'
	import {getFacilityDetail,editFacility} from '../api/getData.js'
  export default {
    name: 'business_edit',
    data: function () {
			let reg = /^[1-9][0-9]{0,2}$/
			let validateCode = (rule, value, callback) => { 
				if (!value) { 
					return callback(new Error('请填写地址码')) 
				} 
				setTimeout(() => { 
					if (!reg.test(value)) { 
						callback(new Error('地址码须为1-999的数字')) 
					} else { 
						callback() 
					} 
				}) 
			}
      return {
        title:"编辑设备",
        name:'',
        time:'',
				id:'',
				windowId:'',
				equipmentType:'',
				form:{
					deviceName:'',
					addressCode:'',
					brand:'',
					manufacturers:'',
					unitType:'',
					remark:''
				},
				rules:{
					equipment:[{required: true, message: '请选择设备类型', trigger: 'change'}],
					deviceName:[{required: true, message: '请输入设备名称', trigger: 'blur'}],
					addressCode:[
						{required: true,validator:validateCode,trigger: ['blur', 'change']}
					]
				}
      }
    },
    components:{
      titleDiv
    },
    created() {
			this.getParams()
		}, // 创建时
    beforeMount: function () {}, // 挂载之前
    mounted() {
			this.getDetail()
		}, // 挂载之后
    beforeUpdate: function () {}, // 数据更新时调用,在渲染之前
    updated: function () {}, // 数据更新后,渲染后调用(禁止)
    beforeDestroy: function () {}, // 实例销毁前调用,解绑中间层的数据传输
    destroyed: function () {}, // 实例销毁后调用
    methods: {
			getParams(){
				this.id = this.$route.query.id 
				this.windowId = this.$route.query.windowId
				console.log(this.id,this.windowId) 
			},
			getDetail(){
				getFacilityDetail({
					id:this.id
				}).then((res) =>{
					console.log(res)
					if(res.data.code == 0){
						this.equipmentType = res.data.data.type
						this.form.deviceName = res.data.data.name
						this.form.addressCode = res.data.data.uniqueldentifier
						this.form.brand = res.data.data.manufacturer
						this.form.unitType = res.data.data.equipmentmodel
						this.form.remark = res.data.data.welcomelanguage
						this.form.manufacturers = res.data.data.manufacturer
						this.time = res.data.data.creatTime
						this.name = res.data.data.personName
					}else{
						this.$message({
							message: res.data.msg,
							type: 'error'
						});
					}
				}).catch((error) =>{
					console.log(error)
				})
			},
			onSubmit(formName){
				console.log(this.form)
				editFacility({
					equipmentUnique:this.form.addressCode,
					equipmentType:this.equipmentType,
					welcomelanguage:this.form.remark,
					NAME:this.form.deviceName,
					manufacturer:this.form.brand,
					equipmentmodel:this.form.unitType,
					id:this.id
				}).then((res) =>{
					console.log(res.data,"修改设备")
					if(res.data.code == 0){
						this.$message({
							message: res.data.msg,
							type: 'success'
						});
						setTimeout(()=>{
							this.$router.push('/equipment/equipmentindex')
						},1000)
					}else{
						this.$message({
							message: res.data.msg,
							type: 'error'
						});
					}
				}).catch((error) =>{
					console.log(error)
				})
			}
		} // 函数
  }
</script>
<style lang='scss'>
  .business_edit {
    box-sizing: border-box;
    .main{
      background: #fff;
      width: 100%;
      margin-top: 3%;
      padding: 5% 20%;
      .textArea{
        textarea{
          min-height: 200px!important;
        }

      }
      .el-form-item__label{
        font-weight: bold;
      }
			.name{
				color: #000;
				font-size: 16px;
				text-align: center;
				font-weight: bold;
			}
      .btn{
        width: 120px;
        height:32px;
        line-height: 1px;
        margin-left: 80px;
      }
    }
  }
</style>
